package edu.pku.hawk.cloud;

import javax.jms.MessageListener;

import edu.pku.hawk.controller.util.Constants;
import edu.pku.hawk.controller.util.MConsumerQueue;
import edu.pku.hawk.controller.util.MQManager;
import edu.pku.hawk.controller.util.MQueue;
import edu.pku.hawk.controller.util.SerResourceList;

import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.messages.Message;
import flex.messaging.services.MessageService;
import flex.messaging.services.ServiceAdapter;
import flex.messaging.util.UUIDUtils;

public class ResourceRequestServiceAdapter extends ServiceAdapter {

	public ResourceRequestServiceAdapter() {
		System.out.println("Adapter initilized");
	}

	public void start() {
		listenQueue();
		JMSListenner.main(null);
	}

	public void stop() {

	}

	
	@Override
	public Object invoke(Message msg) {
		String messageBody = msg.getBody().toString();
		if (messageBody.equals("ResourceRequest")) {
			System.out.println("Adapter received new");
			return null;
		} else {
			System.out.println("Adapter sending message");
			AsyncMessage newMessage = (AsyncMessage) msg;
			MessageService msgService = (MessageService) getDestination()
					.getService();
			msgService.pushMessageToClients(newMessage, true);
		}
		return null;
	}

	
	public void listenQueue() {
		MQManager mgr = null;
		try {
			mgr = MQManager.getInstance();
			MConsumerQueue queue = mgr
					.getConsumerQueue(Constants.Ctrl_Cloud_RscRequest_QueueName);

			final String clientId = UUIDUtils.createUUID();
			final MessageBroker msgBroker = MessageBroker
					.getMessageBroker(null);

			queue.setMessageListener(new MessageListener() {
				@Override
				public void onMessage(javax.jms.Message message) {
					try {
						System.out.println("get Message!!!");
						
						SerResourceList resourceList = (SerResourceList) MQueue
								.toOjbect(message);
						
						AsyncMessage msg = new AsyncMessage();
						msg.setDestination("ResourceRequestServicePush");
						msg.setClientId(clientId);
						msg.setMessageId(UUIDUtils.createUUID());
//						
//						SerResource res = new SerResource();
//						res.setServiceName(resourceList.ResourceList.get(0).getServiceName());
//						
//						for (SerResource resTemp : resourceList.ResourceList) {
////							res.setServiceName(res.getServiceName() + ":"+resTemp.getServiceName());
//							res.setCpu(res.getCpu()+resTemp.getCpu());
//							res.setMem(res.getMem()+resTemp.getMem());
//							res.setDisk(res.getDisk()+resTemp.getDisk());
//							res.setNet(res.getNet()+resTemp.getNet());
//						}
						
						msg.setBody(resourceList);
						
						
						msgBroker.routeMessageToService(msg, null);

					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			});
			
			queue = mgr
			.getConsumerQueue(Constants.Ctrl_Cloud_RscResponse_UI_QueueName);
			
			queue.setMessageListener(new MessageListener() {
				@Override
				public void onMessage(javax.jms.Message message) {
					try {
						System.out.println("get Message!!!");
						
						SerResourceList resource = (SerResourceList) MQueue
								.toOjbect(message);
						
						AsyncMessage msg = new AsyncMessage();
						msg.setDestination("ResourceRequestServicePush");
						msg.setClientId(clientId);
						msg.setMessageId(UUIDUtils.createUUID());

						msg.setBody(resource);
						
						
						msgBroker.routeMessageToService(msg, null);

					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			});

		} catch (Exception ex) {
			ex.printStackTrace();
			if (mgr != null) {
				try {
					mgr.close();
				} catch (Exception eex) {
					ex.printStackTrace();
				}
			}
		}
	}
}